package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.orders.pojo.Goods_Order;

public class Goods_OrderOperation implements Operation {
	private Connection conn;
	private PreparedStatement ps;
	private Goods_Order go;
	private ResultSet rs;
	@Override
	public boolean insert(Object obj) {
		try {
			if(obj instanceof Goods_Order){
				go = (Goods_Order)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("insert into goods_order values (?,?,?,?,?)");
				ps.setInt(1, go.getId());
				ps.setInt(2, go.getGoods_amount());
				ps.setInt(3, go.getGoods().getId());
				ps.setInt(4, go.getOrders().getId());
				ps.setBoolean(5, go.isVisible());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(Object obj) {
		try {
			if(obj instanceof Goods_Order){
				go = (Goods_Order)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update goods_order set goods_amount = ?,goods_id = ?,order_id = ? visible = ? where id = ?");
				ps.setInt(1, go.getGoods_amount());
				ps.setInt(2, go.getGoods().getId());
				ps.setInt(3, go.getOrders().getId());
				ps.setBoolean(4, go.isVisible());
				ps.setInt(5, go.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List select(Object obj) {
		List list = new ArrayList<>();
		GoodsOperation gso = new GoodsOperation();
		OrdersOperation oo = new OrdersOperation();
		try {
			if(obj instanceof Goods_Order){
				go = (Goods_Order)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from goods_order where id = ?");
				ps.setInt(1, go.getId());
				rs = ps.executeQuery();
				while(rs.next()){
					go.setId(rs.getInt("id"));
					go.setGoods_amount(rs.getInt("goods_amount"));
					go.setGoods(gso.selectById(rs.getInt("goods_id")));
					go.setOrders(oo.selectById(rs.getInt("orders_id")));
					list.add(go);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public Goods_Order selectById(int id) {
		GoodsOperation gso = new GoodsOperation();
		OrdersOperation oo = new OrdersOperation();
		go = new Goods_Order();
		try {
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from goods_order where id = ?");
				ps.setInt(1, id);
				rs = ps.executeQuery();
				rs.next();
					go.setId(rs.getInt("id"));
					go.setGoods_amount(rs.getInt("goods_amount"));
					go.setGoods(gso.selectById(rs.getInt("goods_id")));
					go.setOrders(oo.selectById(rs.getInt("orders_id")));
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return go;
	}

	@Override
	public boolean delete(Object obj) {
		try {
			if(obj instanceof Goods_Order){
				go = (Goods_Order)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update goods_order set visible = ? where id = ?");
				ps.setInt(2, go.getId());
				ps.setBoolean(1, false);
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
